package com.huihuiyun.huihuiyun.server.controller.member.client;

import com.huihuiyun.huihuiyun.server.controller.member.client.viewobjects.HhyProductCategoryVO;
import com.huihuiyun.huihuiyun.server.controller.member.client.viewobjects.HhyProductVO;
import com.huihuiyun.huihuiyun.server.domain.HhyProduct;
import com.huihuiyun.huihuiyun.server.domain.HhyProductCategory;
import com.huihuiyun.huihuiyun.server.service.HhyProductCategoryService;
import com.huihuiyun.huihuiyun.server.service.HhyProductService;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVO;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * 商品分类， 商品
 */
@Component
@Path(Const.context)
@Slf4j
public class MemberClientPointsMallController {
    @Autowired
    private HhyProductCategoryService hhyProductCategoryService;

    @Autowired
    private HhyProductService hhyProductService;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/pointsMall/productCategory/list")
    public ResultVO<List<HhyProductCategoryVO>> findPointsMallProductCategoryList() {
        try {
            List<HhyProductCategory> hhyProductCategoryList = hhyProductCategoryService.findPointsMallProductCategoryList();
            return ResultVOUtil.success(MemberControllerHelper.convertUniteMallProductCategory2ViewObject(hhyProductCategoryList));
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取积分商城商品分类失败");
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/pointsMall/product/findProductListByProductCategoryId")
    public ResultVO<List<HhyProductVO>> findProductListByProductCategoryId(@QueryParam("productCategoryId") String productCategoryId) {
        try {
            int status = HhyProduct.statusUp;
            int reviewStatus = HhyProduct.reviewStatus1Success;
            List<HhyProduct> hhyProductList = hhyProductService.findAllByCategoryIdAndStatusAndReviewStatus(productCategoryId, status, reviewStatus);
            List<HhyProductVO> hhyProductVOList = ResultVOUtil.convertList(hhyProductList, HhyProductVO.class);
            return ResultVOUtil.success(hhyProductVOList);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取积分商城商品失败");
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/pointsMall/product/findProductById")
    public ResultVO<HhyProductVO> findProductById(@QueryParam("id") String id) {
        try {
            HhyProduct hhyProduct = hhyProductService.findOne(id);
            HhyProductVO hhyProductVO = ResultVOUtil.convert(hhyProduct, HhyProductVO.class);
            return ResultVOUtil.success(hhyProductVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取积分商城商品失败");
        }
    }
}